<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2016-6-21
 * Func   代理通知
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$table = Table('agent_notice');

if(IS_POST)
{
    $id = intval($_REQUEST['id']);
    $data = $id ? $db->getrow('SELECT * FROM '.$table.' WHERE id = '.$id) : [];

    $ins = [];
    $ins['title']	=   NoBadStr($_POST['title']);
    $ins['content']	=   $_POST['content'];
	$ins['istop'] 	= 	intval($_POST['istop']);
	
    if($data['id'])
	{
        //修改
        $db->exec('UPDATE '.$table.' SET '.CreateUpdateSql($ins).' WHERE id = '.$data['id']);
    }
	else
	{
		$ins['dateline']=   TIMESTAMP;
        $db->exec('INSERT INTO '.$table.CreateInsertSql($ins));
    }
	
    ShowMsg('操作成功',U($m.'/'.$c));
}

if($a == 'add')
{
    $id = intval($_GET['id']);
    $data = $id ? $db->getrow('SELECT * FROM '.$table.' WHERE id = '.$id) : [];

    $tpl->display($m.'/notice_add');
}
else if($a == 'del')
{
    $id = intval($_GET['id']);
	$id && $db->exec('UPDATE '.$table.' SET status = 0 WHERE id = '.$id);
	
    AjaxReturn(['error'=>'0','msg'=>'删除成功']);
}
else
{
    $data  = [];
    $where = ' FROM '.$table.' t WHERE status = 1 ';
	
	$title = NoBadStr($_GET['title']);
	$title && $where .= ' AND title LIKE \'%'.$title.'%\'';
	
    $tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
    if($tmp['p'])
	{
        $page = max(1,intval($_GET['page']));
        $size = 20;
        $limit    = ' LIMIT '.($page - 1) * $size.','.$size;

		$url = '?m='.$m.'&c='.$c.'&title='.$title.'&cid='.$cid;
        $orderby = ' ORDER BY t.id DESC ';
		
        $data = $db->getall('SELECT t.* '.$where.$orderby.$limit);
		
        $multi = Multi($url,$page,$tmp['p'],$size);
    }

    $tpl->display($m.'/notice');
}